package com.haixee.parkdemo.common.response;

import com.haixee.parkdemo.common.context.WebContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 捕获 controller 请求异常
 *
 * @author RUIFENG GUO
 * 2020/3/24 11:18
 */
@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {

    /**
     * 捕获异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<ResResult> doError(HttpServletRequest servletRequest, HttpServletResponse httpServletResponse, Exception ex) {

        ResResult commonRes = ExceptionUtil.getExceptionResponse(ex);
//        if (CommonResEnum.UNKNOWN_ERROR.getCode().equals(commonRes.getCode())) {
//            dingTalkAlert.warn("TARA 服务繁忙", ex.getMessage(), true);
//        }

        WebContextHolder.setResponseCode(commonRes.getCode());
        WebContextHolder.setResponseMsg(commonRes.getMsg());
//        LogUtil.saveResInfo(commonRes);

        // 返回客户端
        return new ResponseEntity<>(commonRes, commonRes.getStatus());
    }
}
